<?php
if (isset($_FILES['file'])) {
    $allowedTypes = array('image/jpeg', 'image/png', 'image/gif'); // 允许上传的文件类型

    $file = $_FILES['file'];
    $uploadDir = '../../images/rooms/updataroom/';

    $originalFileType = $file['type'];
    if (!in_array($originalFileType, $allowedTypes)) {
        echo json_encode(array('error' => '无效的文件类型。只允许使用JPEG、PNG和GIF文件。'));
        exit;
    }
    $originalFileName = $file['name'];
    $extension = pathinfo($originalFileName, PATHINFO_EXTENSION);

    $newFileName = time() . '_' . mt_rand(1000, 9999) . '.' . $extension; // 使用当前时间戳和随机数生成新文件名
    $uploadFile = $uploadDir . $newFileName;

    if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
        $relativePath = 'images/rooms/updataroom/' . $newFileName;
    
        header('Content-Type: application/json');
        echo json_encode(array('newFileName' => $newFileName, 'relativePath' => $relativePath));
    } else {
        echo json_encode(array('error' => '无法上传文件。', 'uploadError' => error_get_last()));
    }
}
?>